﻿using Engine;
using Game;
namespace Mekiasm
{
    public class ElectricDigtalWidget : BaseEnergyWidget
    {
        public bool flag = false;
        public ComponentElectricDigtal Component;
        public CheckboxWidget Checkbox_JZ = new CheckboxWidget() { VerticalAlignment = WidgetAlignment.Center, Margin = new Vector2(10, 0), Text = "精准采集模式" };
        public CheckboxWidget Checkbox_Run = new CheckboxWidget() { VerticalAlignment = WidgetAlignment.Far, Margin = new Vector2(10, 60), Text = "是否运行" };
        public BevelledButtonWidget Config = new BevelledButtonWidget() { Size = new Vector2(100, 36), VerticalAlignment = WidgetAlignment.Far, Margin = new Vector2(10, 30), Text = "白名单配置", FontScale = 0.7f };
        public GridPanelWidget gridPanel = new GridPanelWidget() { HorizontalAlignment = WidgetAlignment.Far, VerticalAlignment = WidgetAlignment.Center };
        public EditTextWidget BlockSearch = new EditTextWidget() { Size = new Vector2(200, 32), HorizontalAlignment = WidgetAlignment.Far, Margin = new Vector2(20, 20) };
        public ListViewWidget SelectList = new ListViewWidget() { Size = new Vector2(200, 32), Margin = new Vector2(20, 54), HorizontalAlignment = WidgetAlignment.Far };
        public GridPanelWidget gridPanelWhiteList = new GridPanelWidget() { HorizontalAlignment = WidgetAlignment.Far, VerticalAlignment = WidgetAlignment.Center, Margin = new Vector2(20, 0) };
        public CanvasWidget CanvasWidget = new CanvasWidget();

        public ElectricDigtalWidget(ComponentElectricDigtal machine, ComponentMiner miner) : base(miner)
        {
            Component = machine;
            gridPanel.ColumnsCount = 4;
            gridPanel.RowsCount = 4;
            gridPanelWhiteList.ColumnsCount = 4;
            gridPanelWhiteList.RowsCount = 2;
            BlockSearch.PlaceHolder = "输入名称搜索方块...";
            BlockSearch.TextChange += (text) =>
            {
                if (string.IsNullOrEmpty(text)) return;
                SelectList.Clear();
                for (int i = 0; i < BlocksManager.Blocks.Length; i++)
                {
                    foreach (int v in BlocksManager.Blocks[i].GetCreativeValues())
                    {
                        if (BlocksManager.Blocks[i].GetDisplayName(null, v).Contains(text))
                        {
                            SelectList.AddItem(v);
                        }
                    }
                }
            };
            SelectList.Adapter = (obj) =>
            {
                int v = (int)obj;
                CanvasWidget canvasWidget = new CanvasWidget() { Size = new Vector2(34) };
                BevelledRectangleWidget bevelled = new BevelledRectangleWidget() { CenterColor = new Color(0, 0, 0, 125), BevelColor = Color.White, BevelSize = 1f, Size = new Vector2(34) };
                MekBlockIconWidget mekBlockIcon = new MekBlockIconWidget() { Size = new Vector2(32), Margin = new Vector2(2, 1), Value = v, HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center };
                canvasWidget.Children.Add(bevelled);
                canvasWidget.Children.Add(mekBlockIcon);
                return canvasWidget;
            };
            SelectList.ItemClicked = (obj) =>
            {
                int v = (int)obj;
                if (Component.WhiteList.Count < 8 && !Component.WhiteList.Contains(v))
                {
                    Component.WhiteList.Add(v);
                }
                Refresh();
            };
            Checkbox_JZ.CheckChange = () =>
            {
                Component.IsPrecise = Checkbox_JZ.IsCheck;
            };
            Checkbox_Run.CheckChange = () =>
            {
                Component.IsRun = Checkbox_Run.IsCheck;
            };
            CanvasWidget.Size = CanvasMain.Size;
            CanvasMain.Children.Add(CanvasWidget);
            InitMain();
            setTitle("数字型挖矿机");
        }

        public void InitMain()
        {
            CanvasWidget.Children.Clear();
            CanvasWidget.Children.Add(gridPanel);
            CanvasWidget.Children.Add(Checkbox_JZ);
            CanvasWidget.Children.Add(Checkbox_Run);
            CanvasWidget.Children.Add(Config);
            Checkbox_JZ.IsCheck = Component.IsPrecise;
            Checkbox_Run.IsCheck = Component.IsRun;
            gridPanel.Children.Clear();
            for (int i = 0; i < gridPanel.ColumnsCount; i++)
            {
                for (int j = 0; j < gridPanel.RowsCount; j++)
                {
                    InventorySlotWidget slotWidget = new InventorySlotWidget() { Size = new Vector2(48), Margin = new Vector2(2) };
                    slotWidget.AssignInventorySlot(Component, j + i * gridPanel.RowsCount);
                    gridPanel.Children.Add(slotWidget);
                    gridPanel.SetWidgetCell(slotWidget, new Point2(j, i));
                }
            }
        }

        public void Refresh()
        {

            gridPanelWhiteList.Children.Clear();
            int num = 0;
            for (int i = 0; i < gridPanelWhiteList.RowsCount; i++)
            {
                for (int j = 0; j < gridPanelWhiteList.ColumnsCount; j++)
                {
                    int v = 0;
                    if (num < Component.WhiteList.Count)
                    {
                        v = Component.WhiteList[num];
                    }
                    CanvasWidget canvasWidget = new CanvasWidget() { Size = new Vector2(50), Margin = new Vector2(2) };
                    BevelledRectangleWidget bevelled = new BevelledRectangleWidget() { CenterColor = new Color(0, 0, 0, 125), BevelColor = Color.White, BevelSize = 1f, Size = new Vector2(48) };
                    MekBlockIconWidget slotWidget = new MekBlockIconWidget() { Size = new Vector2(48), Value = v, HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center };
                    canvasWidget.Children.Add(bevelled);
                    canvasWidget.Children.Add(slotWidget);
                    gridPanelWhiteList.Children.Add(canvasWidget);
                    gridPanelWhiteList.SetWidgetCell(canvasWidget, new Point2(j, i));
                    num++;
                }
            }

        }

        public void InitWhiteList()
        {
            CanvasWidget.Children.Clear();
            CanvasWidget.Children.Add(Checkbox_JZ);
            CanvasWidget.Children.Add(Checkbox_Run);
            CanvasWidget.Children.Add(Config);
            CanvasWidget.Children.Add(gridPanelWhiteList);
            CanvasWidget.Children.Add(BlockSearch);
            CanvasWidget.Children.Add(SelectList);
            Checkbox_JZ.IsCheck = Component.IsPrecise;
            Checkbox_Run.IsCheck = Component.IsRun;
            Refresh();
        }

        public override void Update()
        {
            if (Config.IsClicked)
            {
                if (flag)
                {
                    InitMain();
                    flag = false;
                    Config.Text = "返回";
                }
                else
                {
                    InitWhiteList();
                    flag = true;
                    Config.Text = "白名单配置";
                }

            }

            SetPowerProgress((float)Component.Power / Component.MaxPower);
            SetLeftBottomMoreInfo($"消耗:{ILibrary.FormatPower(Component.OutputPowerLimit)}\n当前储能:{ILibrary.FormatPower(Component.Power)}\n最大:{ILibrary.FormatPower(Component.MaxPower)}");
        }


    }
}
